const { login } = require('../controller/user')
const { successModel, errorModel } = require('../model/resModel')

const handleUserRouter = (req, res) => {
    const method = req.method
    if (method === 'POST' && req.path === '/api/user/login') {
        const { username, password } = req.body
        const result = login(username, password)
        return result.then((data) => {
            if (data.username) {
                //   设置session
                req.session.username = data.username
                req.session.realname = data.realname
                return new successModel()
            } else {
                return new errorModel('登陆失败')
            }
        })
    }

    if (method === 'GET' && req.path === '/api/user/login-test') {
        if (req.session.username) {
            return Promise.resolve(
                new successModel({
                    session: req.session
                })
            )
        }
        return Promise.resolve(
            new errorModel('尚未登陆')
        )
    }
}
module.exports = handleUserRouter